package com.dream.utilservice.anno;

import com.dream.utilservice.utils.Spelparser;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MemberSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@Component
@Aspect//定义一个切面
public class DoSomethingAspect {


    @Around("@annotation(dst)")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint, Testannotation dst) throws Throwable {
        String key = getKey(dst.testkey(),proceedingJoinPoint);
        String name = dst.testname();
        boolean isture = dst.ifture();

        System.out.println("1."+key);
        System.out.println("2."+name);
        System.out.println("3."+isture);
        Object ret = proceedingJoinPoint.proceed();
        return ret;
    }


    private String getKey(String testkey,ProceedingJoinPoint pjp) {
        Method method = ((MethodSignature) (pjp.getSignature())).getMethod();
        Parameter[] parameters = method.getParameters();
        String[] parameterNames = new String[parameters.length];
        for (int i=0;i<parameters.length;i++){
            parameterNames[i]=parameters[i].getName();
        }
        return Spelparser.getKey(testkey,parameterNames ,pjp.getArgs() );
    }
}
